package com.stock.demo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.stock.demo.R;
import com.stock.demo.model.StockBean;

import java.util.ArrayList;

/**
 * Created by XuZhaoyu on 2017/7/1.
 */

public class SelfSelectAdapter extends RecyclerView.Adapter<SelfSelectAdapter.ViewHolder> {

    public static final String TYPE_TITLE = "title";
    public static final String TYPE_CONTENT = "content";
    private Context mContext;
    private ArrayList<StockBean> mData;
    private String mAdapterType;

    public SelfSelectAdapter(Context context, ArrayList<StockBean> data, String adapterType) {
        mContext = context;
        mData = data;
        mAdapterType = adapterType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (TextUtils.equals(mAdapterType, TYPE_TITLE)) {
            view = LayoutInflater.from(mContext).inflate(R.layout.view_self_select_title_body, parent, false);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.view_self_select_content_body, parent, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bindView(holder, position);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private View mItemView;

        private TextView mTvStockName;
        private TextView mTvSymbol;
        private TextView mTvLastTrade;
        private TextView mTvChangePercent;
        private TextView mTvChange;
        private TextView mTvBid;
        private TextView mTvAsk;
        private TextView mTvPreClose;
        private TextView mTvOpen;
        private TextView mTvVolume;
        private TextView mTvTurOver;

        public ViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            switch (mAdapterType) {
                case TYPE_TITLE:
                    mTvStockName = (TextView) mItemView.findViewById(R.id.tv_stock_name);
                    mTvSymbol = (TextView) mItemView.findViewById(R.id.tv_stock_symbol);
                    break;
                case TYPE_CONTENT:
                    mTvLastTrade = (TextView) mItemView.findViewById(R.id.tv_body_last_trade);
                    mTvChangePercent = (TextView) mItemView.findViewById(R.id.tv_body_change_percent);
                    mTvChange = (TextView) mItemView.findViewById(R.id.tv_body_change);
                    mTvBid = (TextView) mItemView.findViewById(R.id.tv_body_bid);
                    mTvAsk = (TextView) mItemView.findViewById(R.id.tv_body_ask);
                    mTvPreClose = (TextView) mItemView.findViewById(R.id.tv_body_pre_close);
                    mTvOpen = (TextView) mItemView.findViewById(R.id.tv_body_open);
                    mTvVolume = (TextView) mItemView.findViewById(R.id.tv_body_volume);
                    mTvTurOver = (TextView) mItemView.findViewById(R.id.tv_body_tur_over);
                    break;
            }

        }

        public void bindView(ViewHolder holder, int position) {
            StockBean selectBean = mData.get(position);
            int colorRed = mContext.getResources().getColor(R.color.colorRed);
            int colorGreen = mContext.getResources().getColor(R.color.colorGreen);
            switch (mAdapterType) {
                case TYPE_TITLE:
                    mTvStockName.setText(selectBean.mName);
                    mTvSymbol.setText(selectBean.mSymbol);
                    if (selectBean.mChange < 0) {
                        mTvStockName.setTextColor(colorGreen);
                        mTvSymbol.setTextColor(colorGreen);
                    } else {
                        mTvStockName.setTextColor(colorRed);
                        mTvSymbol.setTextColor(colorRed);
                    }
                    break;
                case TYPE_CONTENT:
                    mTvLastTrade.setText(String.valueOf(selectBean.mLastTrade));
                    mTvChangePercent.setText(selectBean.mChangePercent);
                    if (selectBean.mChange < 0) {
                        mTvLastTrade.setTextColor(colorGreen);
                        mTvChangePercent.setTextColor(colorGreen);
                        mTvChange.setTextColor(colorGreen);
                        mTvBid.setTextColor(colorGreen);
                        mTvAsk.setTextColor(colorGreen);
                        mTvPreClose.setTextColor(colorGreen);
                        mTvOpen.setTextColor(colorGreen);
                        mTvVolume.setTextColor(colorGreen);
                        mTvTurOver.setTextColor(colorGreen);
                    } else {
                        mTvLastTrade.setTextColor(colorRed);
                        mTvChangePercent.setTextColor(colorRed);
                        mTvChange.setTextColor(colorRed);
                        mTvBid.setTextColor(colorRed);
                        mTvAsk.setTextColor(colorRed);
                        mTvPreClose.setTextColor(colorRed);
                        mTvOpen.setTextColor(colorRed);
                        mTvVolume.setTextColor(colorRed);
                        mTvTurOver.setTextColor(colorRed);
                    }
                    mTvChange.setText(String.valueOf(selectBean.mChange).substring(0, 4));
                    mTvBid.setText(String.valueOf(selectBean.mBid));
                    mTvAsk.setText(String.valueOf(selectBean.mAsk));
                    mTvPreClose.setText(String.valueOf(selectBean.mPreClose));
                    mTvOpen.setText(String.valueOf(selectBean.mOpen));
                    mTvVolume.setText(String.valueOf(selectBean.mVolume));
                    mTvTurOver.setText(String.valueOf(selectBean.mTurOver));
                    break;
            }

        }

    }

}


